/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2024/05/29.
//

#include "sql/expr/aggregator.h"
#include "common/log/log.h"

RC SumAggregator::accumulate(const Value &value)
{
  if (value_.attr_type() == AttrType::UNDEFINED) {
    value_ = value;
    return RC::SUCCESS;
  }
  
  ASSERT(value.attr_type() == value_.attr_type(), "type mismatch. value type: %s, value_.type: %s", 
        attr_type_to_string(value.attr_type()), attr_type_to_string(value_.attr_type()));
  
  Value::add(value, value_, value_);
  return RC::SUCCESS;
}

RC SumAggregator::evaluate(Value& result)
{
  result = value_;
  return RC::SUCCESS;
}

// MAX聚合函数实现
RC MaxAggregator::accumulate(const Value &value)
{
  if (value_.attr_type() == AttrType::UNDEFINED) {
    value_ = value;
    return RC::SUCCESS;
  }
  
  ASSERT(value.attr_type() == value_.attr_type(), "type mismatch. value type: %s, value_.type: %s", 
        attr_type_to_string(value.attr_type()), attr_type_to_string(value_.attr_type()));
  
  if (value.compare(value_) > 0) {
    value_ = value;
  }
  return RC::SUCCESS;
}

RC MaxAggregator::evaluate(Value& result)
{
  result = value_;
  return RC::SUCCESS;
}

// MIN聚合函数实现
RC MinAggregator::accumulate(const Value &value)
{
  if (value_.attr_type() == AttrType::UNDEFINED) {
    value_ = value;
    return RC::SUCCESS;
  }
  
  ASSERT(value.attr_type() == value_.attr_type(), "type mismatch. value type: %s, value_.type: %s", 
        attr_type_to_string(value.attr_type()), attr_type_to_string(value_.attr_type()));
  
  if (value.compare(value_) < 0) {
    value_ = value;
  }
  return RC::SUCCESS;
}

RC MinAggregator::evaluate(Value& result)
{
  result = value_;
  return RC::SUCCESS;
}

// COUNT聚合函数实现
CountAggregator::CountAggregator() : count_(0)
{
  value_.set_int(0);
}

RC CountAggregator::accumulate(const Value &value)
{
  // 对于COUNT函数，无论值是什么都进行计数
  // 假设非空值的attr_type不是UNDEFINED
  if (value.attr_type() != AttrType::UNDEFINED) {
    count_++;
    value_.set_int(count_);
  }
  return RC::SUCCESS;
}

RC CountAggregator::evaluate(Value& result)
{
  result = value_;
  return RC::SUCCESS;
}

// AVG聚合函数实现
AvgAggregator::AvgAggregator() : count_(0)
{
  value_.set_float(0.0);
  sum_.set_float(0.0);
}

RC AvgAggregator::accumulate(const Value &value)
{
  // 假设非空值的attr_type不是UNDEFINED
  if (value.attr_type() == AttrType::UNDEFINED) {
    return RC::SUCCESS;
  }

  if (count_ == 0) {
    sum_ = value;
    if (sum_.attr_type() == AttrType::INTS) {
      value_.set_float(static_cast<float>(sum_.get_int()));
    } else if (sum_.attr_type() == AttrType::FLOATS) {
      value_ = sum_;
    } else {
      LOG_WARN("invalid value type for avg aggregator: %d", sum_.attr_type());
      return RC::INVALID_ARGUMENT;
    }
  } else {
    if (value.attr_type() == AttrType::INTS) {
      if (sum_.attr_type() == AttrType::INTS) {
        sum_.set_int(sum_.get_int() + value.get_int());
      } else if (sum_.attr_type() == AttrType::FLOATS) {
        sum_.set_float(sum_.get_float() + static_cast<float>(value.get_int()));
      }
    } else if (value.attr_type() == AttrType::FLOATS) {
      if (sum_.attr_type() == AttrType::INTS) {
        sum_.set_float(static_cast<float>(sum_.get_int()) + value.get_float());
      } else if (sum_.attr_type() == AttrType::FLOATS) {
        sum_.set_float(sum_.get_float() + value.get_float());
      }
    } else {
      LOG_WARN("invalid value type for avg aggregator: %d", value.attr_type());
      return RC::INVALID_ARGUMENT;
    }
  }
  
  count_++;
  
  // 计算平均值
  if (sum_.attr_type() == AttrType::INTS) {
    value_.set_float(static_cast<float>(sum_.get_int()) / count_);
  } else {
    value_.set_float(sum_.get_float() / count_);
  }
  
  return RC::SUCCESS;
}

RC AvgAggregator::evaluate(Value& result)
{
  if (count_ == 0) {
    // 如果没有记录，设置为0.0
    result.set_float(0.0);
  } else {
    result = value_;
  }
  return RC::SUCCESS;
}
